package com.example.spinnerdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    private Spinner spin1;
    private Spinner spin2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spin1 = findViewById(R.id.spin1);
        spin2 = findViewById(R.id.spin2);

        String[] books = { "初识Android开发", "Android初识开发", "Android中级开发",
                "Android高级开发", "Android开发进阶"};

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1
                            ,books);
        spin2.setAdapter(adapter);

        spin1.setOnItemSelectedListener(this);
        spin2.setOnItemSelectedListener(this);
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String text = parent.getItemAtPosition(position).toString();
        System.out.print("parentId:"+parent.getId()+",id:"+id);
        switch (parent.getId()){
            case R.id.spin1:
                Toast.makeText(MainActivity.this,"Choice:"+text,Toast.LENGTH_LONG).show();;
                break;
            case R.id.spin2:
                Toast.makeText(MainActivity.this,"Choice:"+text,Toast.LENGTH_LONG).show();
                break;
            default: break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
